package com.lpwa.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.lpwa.common.core.text.Convert;
import com.lpwa.common.utils.StringUtils;
import com.lpwa.framework.util.ShiroUtils;
import com.lpwa.system.domain.tagdata.position.Zone;
import com.lpwa.system.mapper.SysDeptMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.system.domain.Area;
import com.lpwa.system.service.IAreaService;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.utils.poi.ExcelUtil;

/**
 * 区域 信息操作处理
 * 
 * @author tanmu
 * @date 2019-08-07
 */
@Controller
@RequestMapping("/system/area")
public class AreaController extends BaseController
{
    private String prefix = "system/area";
	
	@Autowired
	private IAreaService areaService;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@RequiresPermissions("system:area:view")
	@GetMapping()
	public String area()
	{
	    return prefix + "/area";
	}
	
	/**
	 * 查询区域列表
	 */
	@RequiresPermissions("system:area:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Area area)
	{
		startPage();
        List<Area> list = areaService.selectAreaList(area);
        if (list.size()>0){
        	for (Area a:list){
        		//查询组织名称
				String deptIds = a.getAssociationDept();
				if (!StringUtils.isEmpty(deptIds)){
					List<String> deptNameList = sysDeptMapper.selectAreaDeptTree(Convert.toStrArray(deptIds));
					//将deptNameList用逗号拼接
					String deptNames = StringUtils.listToString2(deptNameList,',');
					a.setAssociationDeptName(deptNames);
				}
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出区域列表
	 */
	@RequiresPermissions("system:area:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Area area)
    {
    	List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        return util.exportExcel(list, "area");
    }
	
	/**
	 * 新增区域
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存区域
	 */
	@RequiresPermissions("system:area:add")
	@Log(title = "区域", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave()
	{
		Area area = new Area();
		area.setIsStart("1");
		area.setRuleTime(0);
		area.setRule("1");
		area.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(areaService.insertArea(area));
	}

	/**
	 * 修改区域
	 */
	@GetMapping("/edit/{areaId}")
	public String edit(@PathVariable("areaId") Long areaId, ModelMap mmap)
	{
		Area area = areaService.selectAreaById(areaId);
		mmap.put("area", area);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存区域
	 */
	@RequiresPermissions("system:area:edit")
	@Log(title = "区域", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Area area)
	{		
		return toAjax(areaService.updateArea(area));
	}
	
	/**
	 * 删除区域
	 */
	@RequiresPermissions("system:area:remove")
	@Log(title = "区域", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(areaService.deleteAreaByIds(ids));
	}

	/**
	 * 查询区域信息
	 */
	@PostMapping( "/selectAreaById")
	@ResponseBody
	public AjaxResult selectAreaById(Area area) {
		return AjaxResult.success(areaService.selectAreaById(area.getAreaId()));
	}
	/**
	 * 查询区域信息
	 */
	@PostMapping( "/queryAreaList")
	@ResponseBody
	public AjaxResult queryAreaList(Area area) {
		List<Area> list = areaService.selectAreaList(area);
		List<Zone> zoneList = new ArrayList<Zone>();
		if (list.size()>0){
			for (Area a:list){
					Zone zone = new Zone();
					zone.setId(a.getAreaId().toString());
					zone.setName(a.getAreaName());
					zone.setPolygonData(a.getPointData());
					zone.setLocationLocked(false);
					zone.setVisible(true);
					zone.setColor("#ff0000ff");
					zone.setPolygonHoles(new String[1]);
					zone.setIsStart(a.getIsStart());
					zoneList.add(zone);
				}
			}
		return AjaxResult.success(zoneList);
	}
	/**
	 * 区域状态修改
	 */
	@Log(title = "区域管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:role:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(Area area)
	{
		return toAjax(areaService.changeStatus(area));
	}
}
